import axios from 'axios'
import { Message, MessageQueryParams, MessageSettings } from '@/types/message'
import { PaginationData } from '@/types'

// API基础路径
const API_BASE = '/api/messages'

/**
 * 获取消息列表
 * @param params 查询参数
 * @returns 分页消息数据
 */
export const getMessages = (params: MessageQueryParams) => {
  return axios.get<PaginationData<Message>>(API_BASE, { params })
}

/**
 * 获取未读消息数量
 * @returns 未读消息数量
 */
export const getUnreadCount = () => {
  return axios.get<number>(`${API_BASE}/unread/count`)
}

/**
 * 获取消息详情
 * @param id 消息ID
 * @returns 消息详情
 */
export const getMessage = (id: number) => {
  return axios.get<Message>(`${API_BASE}/${id}`)
}

/**
 * 标记消息为已读
 * @param id 消息ID
 * @returns 操作结果
 */
export const markAsRead = (id: number) => {
  return axios.patch<null>(`${API_BASE}/${id}/read`)
}

/**
 * 标记所有消息为已读
 * @returns 操作结果
 */
export const markAllAsRead = () => {
  return axios.patch<null>(`${API_BASE}/read/all`)
}

/**
 * 删除消息
 * @param id 消息ID
 * @returns 操作结果
 */
export const deleteMessage = (id: number) => {
  return axios.delete<Message>(`${API_BASE}/${id}`)
}

/**
 * 清空所有消息
 * @returns 操作结果
 */
export const clearMessages = () => {
  return axios.delete<null>(`${API_BASE}/clear`)
}

/**
 * 获取消息设置
 * @returns 消息设置
 */
export const getMessageSettings = () => {
  return axios.get<MessageSettings>(`${API_BASE}/settings`)
}

/**
 * 更新消息设置
 * @param settings 消息设置
 * @returns 操作结果
 */
export const updateMessageSettings = (settings: Partial<MessageSettings>) => {
  return axios.put<MessageSettings>(`${API_BASE}/settings`, settings)
} 